# coding=utf-8
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from backstage.temples.utils import view_funcs


class TempleList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request):
        """
        获取施愿处列表
        """
        temple_list = view_funcs.get_temple_list()
        return Response({
            "status": 200,
            "msg": "succes",
            "data": temple_list,
        })

    def post(self, request):
        """
        新增施愿处
        """
        name = request.data.get("name")
        img_url = request.data.get("img_url")
        if not name or not img_url:
            return Response({
                "status": 400,
                "msg": "parameter error",
            })

        view_funcs.create_temple(name, img_url)
        return Response({
            "status": 200,
            "msg": "success",
        })


class TempleDetail(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request, temple_id):
        """
        删除施愿处
        """
        data = view_funcs.get_temple(temple_id)
        return Response({
            "status": 200,
            "msg": "success",
            "data": data,
        })

    def delete(self, request, temple_id):
        """
        删除施愿处
        """
        view_funcs.delete_temple(temple_id)
        return Response({
            "status": 200,
            "msg": "success",
        })

    def put(self, request, temple_id):
        """
        修改施愿处
        """
        name = request.data.get("name")
        img_url = request.data.get("img_url")
        if not name or not img_url:
            return Response({
                "status": 400,
                "msg": "parameter error",
            })

        view_funcs.update_temple(temple_id, name, img_url)
        return Response({
            "status": 200,
            "msg": "success",
        })
